﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace YangtzeFilterTestSystem
{
    public partial class AuxScanningForm : Form
    {

        private System.Timers.Timer FormTimer;

        //private event OPCNewDataReadyEventHandler DownstreamOPC1ReceivedNewDataEvent;
        private event OPCNewDataReadyEventHandler UpstreamOPCReceivedNewDataEvent;

        public AuxScanningForm()
        {
            InitializeComponent();

            FormTimer = new System.Timers.Timer();
            FormTimer.AutoReset = true;
            FormTimer.Interval = 5000;
            FormTimer.Elapsed += new System.Timers.ElapsedEventHandler(FormTimer_Elapsed);
            FormTimer.Enabled = true;

            //DownstreamOPC1ReceivedNewDataEvent = new OPCNewDataReadyEventHandler(OPCNewDataReadyEventResponse);
            UpstreamOPCReceivedNewDataEvent = new OPCNewDataReadyEventHandler(OPCNewDataReadyEventResponse);

            //Dev.DownstreamOPC1.OPCNewDataReadyEvent += DownstreamOPC1ReceivedNewDataEvent;
            Dev.UpstreamOPC.OPCNewDataReadyEvent += UpstreamOPCReceivedNewDataEvent;



            if (GlobalVariables.RunningMode == RUNNING_MODE.REAL)
            {
                /*
                if (Dev.MDriverX.IsServoON)
                {
                    ServoXONOFFBtn.Text = "ServoXOFF";
                }
                else
                {
                    ServoXONOFFBtn.Text = "ServoXON";
                }

                if (Dev.MDriverY.IsServoON)
                {
                    ServoYONOFFBtn.Text = "ServoYOFF";
                }
                else
                {
                    ServoYONOFFBtn.Text = "ServoYON";
                }
                 */
            }
            else if (GlobalVariables.RunningMode == RUNNING_MODE.SIMULATION)
            {
                //Do Nothing......
            }
        }

        public void FormTimer_Elapsed(object source, System.Timers.ElapsedEventArgs e)
        {
            if (GlobalVariables.RunningMode == RUNNING_MODE.REAL)
            {
                ActualFreqLabel.Text = FanDriver.GetActualFrequency().ToString();
                ActualFlowrateLabel.Text = (3669.1521 * Math.Pow(GlobalVariables.NozzleDiameter, 2) * Math.Sqrt(KIMOSensors.CurrentNozzleP)).ToString("F");
            }
            else if (GlobalVariables.RunningMode == RUNNING_MODE.SIMULATION)
            {
                //Do Nothing......
            }
        }

        protected override void OnClosed(EventArgs e)
        {
            //Dev.DownstreamOPC1.OPCNewDataReadyEvent -= DownstreamOPC1ReceivedNewDataEvent;
            Dev.UpstreamOPC.OPCNewDataReadyEvent -= UpstreamOPCReceivedNewDataEvent;
            FormTimer.Enabled = false;
            base.OnClosed(e);
        }

        private void OPCNewDataReadyEventResponse(Remote1104 sender, System.EventArgs e)
        {
            if (sender.ID == OPC_ID.UPSTREAM_OPC)
            {
                uint UpstreamOPC_PN_AT_MPPS = 0;
                //uint DownstreamOPC_PN_AT_MPPS = 0;

                /*
                 * ch1 0.10um--0.15um
                 * ch2 0.15um--0.20um
                 * ch3 0.20um--0.25um
                 * ch4 0.25um--0.30um
                 * ch5 0.30um--0.50um
                 * ch6 0.50um--0.70um
                 * ch7 0.70um--1.00um
                 * ch8 1.00um--INFum
                 */

                switch (GlobalVariables.strMPPS)
                {
                    case Constant.MPPS_010_015UM:
                        UpstreamOPC_PN_AT_MPPS = Dev.UpstreamOPC.PN.ch1;
                        //DownstreamOPC_PN_AT_MPPS = Dev.DownstreamOPC1.PN.ch1;
                        break;
                    case Constant.MPPS_015_020UM:
                        UpstreamOPC_PN_AT_MPPS = Dev.UpstreamOPC.PN.ch2;
                        //DownstreamOPC_PN_AT_MPPS = Dev.DownstreamOPC1.PN.ch2;
                        break;
                    case Constant.MPPS_020_025UM:
                        UpstreamOPC_PN_AT_MPPS = Dev.UpstreamOPC.PN.ch3;
                        //DownstreamOPC_PN_AT_MPPS = Dev.DownstreamOPC1.PN.ch3;
                        break;
                    case Constant.MPPS_025_030UM:
                        UpstreamOPC_PN_AT_MPPS = Dev.UpstreamOPC.PN.ch4;
                        //DownstreamOPC_PN_AT_MPPS = Dev.DownstreamOPC1.PN.ch4;
                        break;
                    case Constant.MPPS_030_050UM:
                        UpstreamOPC_PN_AT_MPPS = Dev.UpstreamOPC.PN.ch5;
                        //DownstreamOPC_PN_AT_MPPS = Dev.DownstreamOPC1.PN.ch5;
                        break;
                    case Constant.MPPS_050_070UM:
                        UpstreamOPC_PN_AT_MPPS = Dev.UpstreamOPC.PN.ch6;
                        //DownstreamOPC_PN_AT_MPPS = Dev.DownstreamOPC1.PN.ch6;
                        break;
                    case Constant.MPPS_070_100UM:
                        UpstreamOPC_PN_AT_MPPS = Dev.UpstreamOPC.PN.ch7;
                        //DownstreamOPC_PN_AT_MPPS = Dev.DownstreamOPC1.PN.ch7;
                        break;
                    case Constant.MPPS_100_INFUM:
                        UpstreamOPC_PN_AT_MPPS = Dev.UpstreamOPC.PN.ch8;
                        //DownstreamOPC_PN_AT_MPPS = Dev.DownstreamOPC1.PN.ch8;
                        break;
                    default:
                        break;
                }
                
                //上游的粒子数浓度更新。。。。。。
                UpConcentrationTBox.Text = (UpstreamOPC_PN_AT_MPPS / (GlobalVariables.DilutionRatio) / (28.3 * 1000 / 60)).ToString("N2");
            }
        }

        private void ACS510PictureBox_Click(object sender, EventArgs e)
        {
            ACS510Form ABBACS510Form = new ACS510Form();

            //变频器控制只可弹出一个;
            ABBACS510Form.ShowDialog();
        }

        private void UpPumpStartStopBtn_Click(object sender, EventArgs e)
        {
            try
            {

                if (UpPumpStartStopBtn.Text == "UpPumpStart")
                {
                    PLC.Open(Constant.SSR_PUMP_UPSTREAM);

                    UpPumpStartStopBtn.Text = "UpPumpStop";
                }
                else if (UpPumpStartStopBtn.Text == "UpPumpStop")
                {
                    PLC.Close(Constant.SSR_PUMP_UPSTREAM);

                    UpPumpStartStopBtn.Text = "UpPumpStart";
                }
            }
            catch (System.Exception ex)
            {
                //MessageBox.Show(ex.ToString());
                GlobalVariables.log.Error("Error occured at UpPumpStartStopBtn_Click Function ...." + ex.ToString());
            }

        }

        private void DownPumpStartStopBtn_Click(object sender, EventArgs e)
        {
            if (DownPumpStartStopBtn.Text == "DownPumpStart")
            {
                PLC.Open(Constant.SSR_PUMP_DOWNSTREAM1);

                DownPumpStartStopBtn.Text = "DownPumpStop";
            }
            else if (DownPumpStartStopBtn.Text == "DownPumpStop")
            {
                PLC.Close(Constant.SSR_PUMP_DOWNSTREAM1);

                DownPumpStartStopBtn.Text = "DownPumpStart";
            }

        }

        private void Remote1104Btn_Click(object sender, EventArgs e)
        {
            Remote1104Form remote1104 = new Remote1104Form();
            remote1104.ShowDialog();
        }

        private void ServoXONOFFBtn_Click(object sender, EventArgs e)
        {
            if (GlobalVariables.RunningMode == RUNNING_MODE.REAL)
            {
                if (ServoXONOFFBtn.Text == "ServoXON")
                {
                    if (!Dev.MDriverX.IsServoON)
                    {
                        Dev.MDriverX.ServoON();

                        ServoXONOFFBtn.Text = "ServoXOFF";
                    }
                    else
                    {
                        Dev.MDriverX.ServoOFF();
                    }
                }
                else if (ServoXONOFFBtn.Text == "ServoXOFF")
                {
                    if (Dev.MDriverX.IsServoON)
                    {
                        Dev.MDriverX.ServoOFF();

                        ServoXONOFFBtn.Text = "ServoXON";
                    }
                }
            }
            else if (GlobalVariables.RunningMode == RUNNING_MODE.SIMULATION)
            {
                //Do Nothing....
            }
        }

        private void ServoYONOFFBtn_Click(object sender, EventArgs e)
        {
            if (GlobalVariables.RunningMode == RUNNING_MODE.REAL)
            {
                if (ServoYONOFFBtn.Text == "ServoYON")
                {
                    if (!Dev.MDriverY.IsServoON)
                    {
                        Dev.MDriverY.ServoON();

                        ServoYONOFFBtn.Text = "ServoYOFF";
                    }
                    else
                    {
                        Dev.MDriverY.ServoOFF();
                    }
                }
                else if (ServoYONOFFBtn.Text == "ServoYOFF")
                {
                    if (Dev.MDriverY.IsServoON)
                    {
                        Dev.MDriverY.ServoOFF();

                        ServoYONOFFBtn.Text = "ServoYON";
                    }
                }
            }
            else if (GlobalVariables.RunningMode == RUNNING_MODE.SIMULATION)
            {
                //Do Nothing......
            }
        }

        private void InstantStopBtn_Click(object sender, EventArgs e)
        {
            Dev.MDriverX.InstantaneousStop();
            //ScanningThread.Abort();
        }

        private void Motor2InstantStopBtn_Click(object sender, EventArgs e)
        {
            Dev.MDriverY.InstantaneousStop();
            //ScanningThread.Abort();
        }

        private void UpConcentrationMonitorBtn_Click(object sender, EventArgs e)
        {
            
            Dev.UpstreamOPC.SimpleConfigure();
            Dev.UpstreamOPC.InstrumentStart();
            Dev.UpstreamOPC.PredefinedSampleCount = -1;
            
             
            
            //Dev.DownstreamOPC1.SimpleConfigure();
            //Dev.DownstreamOPC1.InstrumentStart();
            //Dev.DownstreamOPC1.PredefinedSampleCount = -1;
             
        }

        private void StopMonitorBtn_Click(object sender, EventArgs e)
        {
            Dev.UpstreamOPC.InstrumentStop();

            //Dev.DownstreamOPC1.InstrumentStop();
        }

    }
}
